/**
 *******************************************************************************
 * @brief       Provide a brief description of the module here (Single line).
 * 
 * @author      Ashwin
 * 
 * @file        Ds1307.h
 * 
 * @version
 * 
 * @date        Feb 21, 2012 7:12:42 PM
 * 
 * @details     Provide a detailed description of the module here (Multi-line).
 * 
 * @pre         Mention any preconditions required to use this module.
 * 
 * @bug         List any bugs in this module.
 * 
 * @note        List any odd modes while using this module.
 * 
 * @warning     List any warnings against using the objects of this module.
 *******************************************************************************
 * @copyright 2012
 * <a href="http://www.afflatustech.com">Afflatus Techtronics</a>
 * and <a href="http://www.ashwinvijayakumar.co.nr">Ashwin Vijayakumar</a>
 * @n@n
 * This work is licensed under the Creative Commons
 * Attribution-NonCommercial-ShareAlike 3.0 Unported License. To view a copy
 * of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or
 * send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain
 * View, California, 94041, USA.
 *******************************************************************************
 */

/**@cond Include guard symbol - Protect against multiple inclusions  @endcond */

#ifndef PAL_DS1307_H_
#define PAL_DS1307_H_

/**@cond ***********************************************************************
 * Add #include "Ds1307.h" in source files that need access to the below 
 * listed declarations.
 ******************************************************************* @endcond */

/**@cond -----------------------------------------------------------------------
 * Includes section
 * Add all #include here.
 * ----------------------------------------------------------------- @endcond */

/**@cond System headers -------------------------------------------- @endcond */

/**@cond Local headers --------------------------------------------- @endcond */
#include "Std_Types.h"

/**@cond -----------------------------------------------------------------------
 * typedef section (Project scope)
 * Add typedef's with project scope here.
 * Capitalization: Upper_Camel_Case.
 * ----------------------------------------------------------------- @endcond */

/* Indexing based on tm_wday (GCC's time.h) */
typedef enum
{
  SUNDAY        = 0x00,
  MONDAY        = 0x01,
  TUESDAY       = 0x02,
  WEDNESDAY     = 0x03,
  THURSDAY      = 0x04,
  FRIDAY        = 0x05,
  SATURDAY      = 0x06,
  NO_OF_WDAY    = 0x07
} PalDs1307_DayOfWeekType;

typedef enum
{
  AM    = 0x00,
  PM    = 0x01
} PalDs1307_AmPmType;

typedef enum
{
  _24_HOUR    = 0x00,
  _12_HOUR    = 0x01
} PalDs1307_HourModeType;

typedef struct tm       PalDs1307_BreakdownTimeType;

typedef char PalDs1307_BreakdownTimeASCIIType;

/* FIXME: Move this typedef to I2C library */
/* I2C Device Address */
/** @brief I2C Device Address
 * @details
 */
typedef unsigned char           I2c_AddressType;

/* DS1307 Address Type */
/** @brief DS1307 Address Type
 * @details This type shall be used to declare variables to hold the DS1307
 * memory location address
 */
typedef unsigned char           PalDs1307_AddressType;

/**
 * @brief DS1307 SECONDS register type.
 * @details This type shall be used to declare a variable which will hold the
 * contents of DS1307's SECONDS register.
 */
typedef union
{
  uint8 byte;
  struct
  {
    uint8 seconds       :7;     /**< Entry Mode Set, S: Shift display. */
    uint8 ch   :1;     /**< Entry Mode Set, I/D: Increment/Decrement cursor position. */
  } bits;
} PalDs1307_SecondsRegType;

/**
 * @brief DS1307 Minutes register type.
 * @details This type shall be used to declare a variable which will hold the
 * contents of DS1307's Minutes register.
 */
typedef union
{
  uint8 byte;
  struct
  {
    uint8                       minutes                 : 7;
    uint8                       reserved_minutes_7      : 1;
  } bits;
} PalDs1307_MinutesRegType;

/**
 * @brief DS1307 HOUR Register type.
 * @details This type shall be used to declare a variable which will hold the
 * contents of DS1307's HOUR register.
 */
typedef union
{
  uint8 byte;
  struct
  {
    uint8 hours       :5;     /**< Entry Mode Set, S: Shift display. */
    PalDs1307_AmPmType ampm   :1;     /**< Entry Mode Set, I/D: Increment/Decrement cursor position. */
    uint8 mode       :1;     /**< Display On/Off Control, B: Blink cursor. */
    uint8 reserved_8    : 1;
  } bits;
} PalDs1307_HourRegType;

/**
 * @brief DS1307 Day register type.
 * @details This type shall be used to declare a variable which will hold the
 * contents of DS1307's Minutes register.
 */
typedef union
{
  uint8 byte;
  struct
  {
    uint8                       day                     : 3;
    uint8                       reserved_3_7        : 5;
  } bits;
} PalDs1307_DayRegType;

/**
 * @brief DS1307 Date register type.
 * @details This type shall be used to declare a variable which will hold the
 * contents of DS1307's Date register.
 */
typedef union
{
  uint8 byte;
  struct
  {
    uint8                       date                    : 6;
    uint8                       reserved_6_7       : 2;
  } bits;
} PalDs1307_DateRegType;

/**
 * @brief DS1307 Month register type.
 * @details This type shall be used to declare a variable which will hold the
 * contents of DS1307's Month register.
 */
typedef union
{
  uint8 byte;
  struct
  {
    uint8                       month                   : 5;
    uint8                       reserved_5_7      : 3;
  } bits;
} PalDs1307_MonthRegType;

/**
 * @brief DS1307 Year register type.
 * @details This type shall be used to declare a variable which will hold the
 * contents of DS1307's Year register.
 */
typedef uint8                 PalDs1307_YearRegType;

/**
 * @brief DS1307 CONTROL Register Type.
 * @details This type shall be used to declare a variable which will hold the
 * contents of DS1307's CONTROL register.
 */
typedef union
{
  uint8 byte;
  struct
  {
    uint8 rs        :2;     /**< Function Set, F: Font. */
    uint8 reserved_2_3    : 2;
    uint8 sqwe      :1;     /**< Display On/Off Control, C: Show cursor. */
    uint8 reserved_5_6    : 2;
    uint8 out       :1;     /**< Display On/Off Control, B: Blink cursor. */
  } bits;
} PalDs1307_ControlRegType;

/**
 * @brief DS1307 Timekeeper Register Type.
 * @details This type shall be used to declare a variable which will hold the
 * Timekeeper Register contents for DS1307.
 */
typedef struct
{
  PalDs1307_SecondsRegType         SecondsReg;        /**< Function Set, F: Font. */
  PalDs1307_MinutesRegType         MinutesReg;        /**< Function Set, F: Font. */
  PalDs1307_HourRegType            HoursReg;          /**< Display On/Off Control, C: Show cursor. */
  PalDs1307_DayRegType             DayReg;            /**< Display On/Off Control, C: Show cursor. */
  PalDs1307_DateRegType            DateReg;           /**< Function Set, F: Font. */
  PalDs1307_MonthRegType           MonthReg;          /**< Display On/Off Control, C: Show cursor. */
  PalDs1307_YearRegType            YearReg;           /**< Function Set, F: Font. */
  uint8                         Reserved;
} PalDs1307_TimekeeperRegType;

/**
 * @brief DS1307 Configuration Type.
 * @details This type shall be used to declare a variable which will hold the
 * configuration parameters for DS1307.
 */
typedef struct
{
  PalDs1307_HourRegType            HoursReg;          /**< Display On/Off Control, C: Show cursor. */
  PalDs1307_ControlRegType         ControlReg;        /**< Display On/Off Control, C: Show cursor. */
} PalDs1307_ConfigType;

/**@cond -----------------------------------------------------------------------
 * Constants section (Project scope)
 * Add all constants with project scope here.
 * Capitalization: ALL_CAPS.
 * ----------------------------------------------------------------- @endcond */

/* TODO: Move this typedef to I2C Library */
#define I2C_READ_WRITE_MSK      0x01

#define PAL_DS1307_HALT_CLK         1
#define PAL_DS1307_RUN_CLK          0

#define PAL_DS1307_BREAKDOWN_ASCII_WDAY        0
#define PAL_DS1307_BREAKDOWN_ASCII_MON         4
#define PAL_DS1307_BREAKDOWN_ASCII_MDAY        8
#define PAL_DS1307_BREAKDOWN_ASCII_HOUR        11
#define PAL_DS1307_BREAKDOWN_ASCII_MIN         14
#define PAL_DS1307_BREAKDOWN_ASCII_SEC         17
#define PAL_DS1307_BREAKDOWN_ASCII_YEAR        20

/**@cond -----------------------------------------------------------------------
 * Macros section (Project scope)
 * Add all macros with project scope here.
 * Capitalization: ALL_CAPS.
 * ----------------------------------------------------------------- @endcond */

/**@cond -----------------------------------------------------------------------
 * Global variables section (Project scope)
 * Add global variables with project scope here. 
 * Capitalization: lower_case.
 * Declare them with 'extern' keyword.
 * ----------------------------------------------------------------- @endcond */

/**@cond -----------------------------------------------------------------------
 * Function prototypes section (Project scope)
 * Add prototypes for all functions with project scope here. 
 * Capitalization: lower_Camel_Case.
 * ----------------------------------------------------------------- @endcond */

/**@cond -----------------------------------------------------------------------
 * Getter/setter prototype section (Project scope)
 * Add prototypes for getter/setter methods here.
 * Capitalization: get_Camel_Case/is_Camel_Case/set_Camel_Case. 
 * ----------------------------------------------------------------- @endcond */

PalDs1307_AmPmType get_PalDs1307_AmPm(void);
void set_PalDs1307_AmPm(PalDs1307_AmPmType ampm);
uint8 PalDs1307_ByteRead (PalDs1307_AddressType Ds1307Address);
Std_ReturnType PalDs1307_ByteWrite (PalDs1307_AddressType Ds1307Address, uint8 DataByte);
void PalDs1307_Init (PalDs1307_ConfigType* PalDs1307_ConfigPtr);
Std_ReturnType PalDs1307_SetHourMode (PalDs1307_HourModeType PalDs1307_HourMode);
Std_ReturnType PalDs1307_SetTime (PalDs1307_BreakdownTimeType* TimePtr);
Std_ReturnType PalDs1307_SetDate (PalDs1307_BreakdownTimeType* TimePtr);
Std_ReturnType PalDs1307_GetTime (PalDs1307_BreakdownTimeType* TimePtr);
Std_ReturnType PalDs1307_GetDate (PalDs1307_BreakdownTimeType* TimePtr);

#endif /* PAL_DS1307_H_ */


/** END OF FILE ********************* Copyright (C) 2012 Afflatus Techtronics */
